package com.tarry.shop;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.ArrayList;
import java.util.List;

public class CodeGenerator {

    public static void main(String[] args) {
        // 1.全局配置
        GlobalConfig config = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        config.setActiveRecord(true)
                .setAuthor("TARRY")// 作者
                .setOutputDir(projectPath + "/shop-generator/src/main/java")// 生成路径
                .setFileOverride(false)// 文件覆盖
                .setIdType(IdType.ASSIGN_ID)
                .setControllerName("%sController")
                .setServiceName("I%sService") // 设置生成service
                .setServiceImplName("%sServiceImpl")
                .setMapperName("I%sMapper")
                .setXmlName("%sMapper")
                // IEmployeeService
                .setBaseResultMap(true)
                .setBaseColumnList(true)
                .setSwagger2(true);

        // 2.数据源配置
        DataSourceConfig dsConfig = new DataSourceConfig();
        dsConfig.setDbType(DbType.MYSQL).setDriverName("com.mysql.jdbc.Driver")
                .setUrl("jdbc:mysql://127.0.0.1:3306/shop_db?serverTimezone=UTC&useSSL=false")
                .setUsername("shop")
                .setPassword("shop");

        List<String> tables = new ArrayList<>();
        tables.add("t_goods");
        tables.add("t_goods_image");

        // 3.策略配置
        StrategyConfig stConfig = new StrategyConfig();
        stConfig.setCapitalMode(true)
                .setNaming(NamingStrategy.underline_to_camel)
                .setEntityLombokModel(true)
                //去除前缀
                .setTablePrefix("t_")
                //需要生成的表
                .setInclude(tables.toArray(new String[]{}));

        TemplateConfig templateConfig = new TemplateConfig();
        //不生成xml文件
        //templateConfig.setXml(null);
        //不生成Controller
        //templateConfig.setController(null);

        // 4.包名策略配置
        PackageConfig pkConfig = new PackageConfig();
        pkConfig.setParent("com.tarry.shop.search.goods").setMapper("mapper").setService("service")
                .setServiceImpl("service.impl").setEntity("entity").setXml("mapper");
        // 5.整合配置
        AutoGenerator ag = new AutoGenerator();
        ag.setGlobalConfig(config).setDataSource(dsConfig).setStrategy(stConfig).setPackageInfo(pkConfig)
                .setTemplateEngine(new FreemarkerTemplateEngine()).setTemplate(templateConfig);
        // 6.执行
        ag.execute();
    }
}
